(function(b, a) {
    function c(f, e) {
        var d = {
            isLead: false,
            leadWord: null,
            words: null,
            colors: ["#cc5b34", "#c27c4d"],
            isFixedWidth: true,
            width: 1000,
            height: 200
        };
        this.options = a.extend(false, d, e);
        this.$wrapper = a(f);
        if (!this.$wrapper || this.options.words.length < 1) {
            return false
        }
        this._create();
        this._bindListener();
        return this
    }
    c.prototype = {
        words: [],
        colors: [],
        colorPos: 0,
        _create: function() {
            if (this.options.isLead && this.options.leadWord) {
                this.words = [this.options.leadWord].concat(this._randArray(this.options.words))
            } else {
                this.words = this._randArray(this.options.words)
            } if (this.options.isFixedWidth) {
                this.$wrapper.width(this.options.width);
                this.$wrapper.height(this.options.height)
            } else {
                this.$wrapper.width(this.$wrapper.parent().width());
                this.$wrapper.height(this.$wrapper.parent().height())
            }
            this.fillRect(this.$wrapper, 0, 0, this.$wrapper.width(), this.$wrapper.height(), this.words)
        },
        fillRect: function(r, g, k, m, f, q) {
            var n = q.length,
                i = m / f,
                e = this._randRange(1, 2, 0.5),
                o = Math.round(n * e[0]),
                p = n - o;
            if (n == 1) {
                this._createBox(r, g, k, m, f, q[0], this._getNextColor());
                return
            }
            if (o == 0) {
                o = 1;
                p--
            } else {
                if (p == 0) {
                    p = 1;
                    o--
                }
            } if (i >= 2.5) {
                var h = Math.round(m * e[0]),
                    j = m - h;
                this.fillRect(r, g, k, h, f, q.slice(0, o));
                this.fillRect(r, g + h, k, j, f, q.slice(o))
            } else {
                var l = Math.round(f * e[0]),
                    d = f - l;
                this.fillRect(r, g, k, m, l, q.slice(0, o));
                this.fillRect(r, g, k + l, m, f - l, q.slice(o))
            }
        },
        _createBox: function(o, g, k, l, e, m, d) {
            var i = e,
                j = 0,
                n = this._getWordsWidth(m.title);
            if (n > l) {
                var h = Math.ceil(n / l);
                i = parseInt(this.$wrapper.css("font-size"));
                j = Math.max(0, (e - h * i) / 2);
                e -= j
            }
            var f = '<div class="box" style="width:' + l + "px;height:" + e + "px;line-height:" + i + "px;top:" + k + "px;left:" + g + "px;background-color:" + d + ";" + (j ? ("padding-top:" + j + "px;") : "") + '"><a href="' + m.url + '" >' + m.title + "</a></div>";
            a(o).append(f)
        },
        _randRange: function(d, h, j) {
            var e = d / h,
                g = e * (1 - j),
                f = e * (1 + j),
                i = Math.random() * (f - g) + g;
            if (h == 1) {
                return [d]
            }
            return [i].concat(this._randRange(d - i, h - 1, j))
        },
        _getNextColor: function() {
            var d = this.options.colors[this.colorPos % this.options.colors.length];
            this.colorPos++;
            return d
        },
        _getWordsWidth: function(d) {
            if (a("#get_ww").size() < 1) {
                a('<div id="get_ww" style="display:block;visibility:hidden;font-size:' + this.$wrapper.css("font-size") + 'px"><span></span></div>').appendTo("body")
            }
            a("#get_ww span").html(d);
            return a("#get_ww span").width()
        },
        _randArray: function(d) {
            var e = d.slice(),
                j = [],
                h;
            for (var f = 0, g = d.length; f < g; f++) {
                h = Math.floor(Math.random() * e.length);
                var k = e[0];
                e[0] = e[h];
                e[h] = k;
                j.push(e[0]);
                e = e.slice(1)
            }
            return j
        },
        _bindListener: function() {
            if (!this.options.isFixedWidth) {
                var d = this,
                    e = null;
                a(b).bind("resize", function() {
                    if (e) {
                        clearTimeout(b.timer);
                        e = null
                    }
                    e = setTimeout(function() {
                        if (d.$wrapper.width() != d.$wrapper.parent().width() || d.$wrapper.height() != d.$wrapper.parent().height()) {
                            d.$wrapper.width(d.$wrapper.parent().width());
                            d.$wrapper.height(d.$wrapper.parent().height());
                            d.$wrapper.empty();
                            d.fillRect(d.$wrapper, 0, 0, d.$wrapper.width(), d.$wrapper.height(), d.words)
                        }
                    }, 800)
                })
            }
        }
    };
    b.WordBox = c;
    a.fn.wordbox = function(e) {
        var d = new c(this, e);
        if (!d) {
            return null
        }
        return a(this)
    }
})(window, jQuery);